package chap04;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 class Date_Demo {
    public static void main(String [] args){
    Date_demo_1();
        System.out.println("SimpleDateFormat");
        simpleDateFormat_demo();
        System.out.println("Calendar");
        Calendar();
    }
    public static void Date_demo_1(){
    Date today=new Date();
    System.out.println("today's date is "+today);
    String strDate,strTime="";
    System.out.println("今天的日期是"+today);
    long time=today.getTime();
    System.out.println("计算一直以来的时间"+time);
    strDate=today.toString();
    strTime=strDate.substring(11,(strDate.length()-4));
    strTime="时间:"+strTime;
    System.out.println(strTime);
    }
    public static  void simpleDateFormat_demo(){
        Date nowtime=new Date();
        System.out.println(nowtime);
        SimpleDateFormat matter1= new SimpleDateFormat("'time':yyyy年MM月dd日E北京时间");
        System.out.println(matter1.format(nowtime));
        SimpleDateFormat matter2 = new SimpleDateFormat("北京时间:yyyy年MM月dd日HH时mm分ss秒");
        Date date1=new Date(1000),date2=new Date(-1000);
        System.out.println(matter2.format(date1));
        System.out.println(matter2.format(date2));
    }
    public static void Calendar(){
        Calendar cal= Calendar.getInstance();
        cal.setTime(new Date());
        String year=String.valueOf(cal.get(Calendar.YEAR)),
                month=String.valueOf(cal.get(Calendar.MONTH)+1),
                day=String.valueOf(cal.get(Calendar.DAY_OF_MONTH)),
                week=String.valueOf(cal.get(Calendar.DAY_OF_WEEK));
        int hour=cal.get(Calendar.HOUR_OF_DAY),
                minute=cal.get(Calendar.MINUTE),
                second=cal.get(Calendar.SECOND);
            System.out.println("现在时间是");
            System.out.println(""+year+"year"+month+"month"+day+"day"+"星期"+week);
            System.out.println(""+hour+"时"+minute+"分"+second+"秒");
            cal.set(1985,9,29);
            long time1985=cal.getTimeInMillis();
            cal.set(2009,9,29);
            long time2009=cal.getTimeInMillis();
            long day_num=(time2009-time1985)/(1000*60*60*26);
            System.out.println("两者的时间对比"+day_num);
    }

}
